/*
 * ShowDateTime.java
 * Please See SimpleDateFormat
 * http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html
 * Created on 2007年5月17日, 上午 7:57
 *
 */
 
import java.io.*;
import java.util.*;
import java.text.DecimalFormat;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
 
/**
 *
 * @author  RYANPAI
 *
 */
public class ShowDateTime {
    
    /** Creates a new instance of ShowDateTime */
    public ShowDateTime() {
        
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        
        // 今天 
        System.out.println("showSerial = "+getDateBySerial());
        
        // 相差幾天
        System.out.println("20070517 ~ 20060731...相差天數..."+calcutePeriod("20070517","20060901"));
        
        // 20天前日期
        System.out.println("20070517...20天前..."+calcuteBeforebyStr("20070517",20));
        
        // 20天後日期
        System.out.println("20070517...20天後..."+calcuteAfterbyStr("20070517",20));
        
        // 是否閏年
        System.out.println("是否閏年...");
        System.out.println("1900..."+isBissextile("1900")); // false
        System.out.println("2000..."+isBissextile("2000")); // true
        System.out.println("2007..."+isBissextile("2007")); // false
        
    }
    
    
    // ****************************************************************************
    // * 回傳系統日 yyyy/MM/dd
    // ****************************************************************************
    private static String getDate() {
        SimpleDateFormat sdf =  new SimpleDateFormat("yyyy/MM/dd");
        java.util.Date today = new Date();
        return String.valueOf(sdf.format(today));
    }
    
    // ****************************************************************************
    // * 回傳系統日 yyyy-MM-dd
    // ****************************************************************************
    public static String getDateByDash() {
        SimpleDateFormat sdf =  new SimpleDateFormat("yyyy-MM-dd");
        java.util.Date today = new Date();
        return String.valueOf(sdf.format(today));
    }
    
    
    // ****************************************************************************
    // * 回傳系統日 yyMMdd
    // ****************************************************************************
    private static String getDateInShort() {
        SimpleDateFormat sdf =  new SimpleDateFormat("yyMMdd");
        java.util.Date today = new Date();
        return String.valueOf(sdf.format(today));
    }
    
    // ****************************************************************************
    // * 回傳系統日 yyyy-MMM-dd (java.util.Locale.US)
    // ****************************************************************************
    private static String getDateBySerial() {
        SimpleDateFormat sdf =  new SimpleDateFormat("yyyy-MMM-dd",java.util.Locale.US);
        java.util.Date today = new Date();
        return String.valueOf(sdf.format(today));
    }
    
    // ****************************************************************************
    // * 回傳系統日至時分秒 yyyyMMddThhmmss*//
    // ****************************************************************************
    private static String getDateTimeStrWithT() {
        Calendar now = Calendar.getInstance();
        long ndate = now.get(Calendar.YEAR)*10000+(now.get(Calendar.MONTH)+1)*100+
        now.get(Calendar.DAY_OF_MONTH);
        long ntime = now.get(Calendar.HOUR_OF_DAY)*10000+now.get(Calendar.MINUTE)*100+
        now.get(Calendar.SECOND);
        return String.valueOf(ndate)+"T"+ String.valueOf(ntime);
    }
    
    // ****************************************************************************
    // * 計算兩日期區間差異回傳??天*//
    // ****************************************************************************
    private static long calcutePeriod(String dateStr1,String dateStr2) {
        SimpleDateFormat sdf =  new SimpleDateFormat("yyyyMMdd");
        ParsePosition pos1 = new ParsePosition(0);
        ParsePosition pos2 = new ParsePosition(0);
        java.util.Date date1= sdf.parse(dateStr1,pos1);
        java.util.Date date2= sdf.parse(dateStr2,pos2);
        long dateDiff = 0;
        if(date1.getTime().getTime()) {
            dateDiff = ((date2.getTime()/1000-date1.getTime()/1000)/(24*60*60));
        }
        else {
            dateDiff = ((date1.getTime()/1000-date2.getTime()/1000)/(24*60*60));
        }
        return dateDiff;
    }
    
    // ****************************************************************************
    // * 計算??天前回傳日期字串 *//
    // ****************************************************************************
    private static String calcuteBeforebyStr(String dateStr1,int bef) {
        SimpleDateFormat sdf =  new SimpleDateFormat("yyyyMMdd");
        ParsePosition pos1 = new ParsePosition(0);
        java.util.Date date1= sdf.parse(dateStr1,pos1);
        java.util.Date date2 = new Date((date1.getTime()/1000-(bef*24*60*60))*1000);
        return String.valueOf(sdf.format(date2));
    }
    
    // ****************************************************************************
    // * 計算??天後回傳日期字串 *//
    // ****************************************************************************
    private static String calcuteAfterbyStr(String dateStr1,int aft) {
        SimpleDateFormat sdf =  new SimpleDateFormat("yyyyMMdd");
        ParsePosition pos1 = new ParsePosition(0);
        java.util.Date date1= sdf.parse(dateStr1,pos1);
        java.util.Date date2 = new Date((date1.getTime()/1000+(aft*24*60*60))*1000);
        return String.valueOf(sdf.format(date2));
    }
    
    // ****************************************************************************
    // * 依base_month取得當月份第一天 : yyyyMM01 *//
    // ****************************************************************************
    private static String getFirstDate(String base_month) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        ParsePosition pos1 = new ParsePosition(0);
        base_month = base_month +"01";
        java.util.Date FirstDate= sdf.parse(base_month,pos1);
        return String.valueOf(sdf.format(FirstDate));
    }
    
    // ****************************************************************************
    // * 回傳是否閏年 *// base_year = yyyy
    // ****************************************************************************
    private static boolean isBissextile(String base_year) {        
        boolean checkIsBissextile = false;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        ParsePosition pos1 = new ParsePosition(0);
        String base_year_0228 = base_year +"0228";
        String base_year_0301 = base_year +"0301";
        if(calcutePeriod(base_year_0228,base_year_0301)==2) checkIsBissextile = true;
        return checkIsBissextile;
    }
            
}
 